Ext.namespace("GridTree");
GridTree.list = function(pp,ids){
	this.parentPanel = pp;
	this.ids={ids:this.ids};
	
	this.treepanel = new Ext.tree.TreePanel({
    	autoScroll : true,
    	collapsible:true,
    	useArrows:true,
    	animate:true,
    	width:250,
    	region:'center',
    	frame:true,
    	split:true,
    	checkModel: 'single',
    	loader : new Ext.tree.TreeLoader( {
            dataUrl : '../gridareas/listAllRegion.form'
        }),
        root: {
            id: '0',
			text: '东区分公司',
			//iconCls: 'icon-group',
			expanded:true
			//checked:false
        },
   	   listeners:{
			click:function(node, event){
				if (node.leaf == false) {//不是子节点
					if (node.expanded == false) {
						node.expand();// 展开
					} else {
						node.collapse();// 收起
					}
				 }
			},
			checkchange:function(node, checked){
				var parentNode = node.parentNode;
	            node.expand();// 展开
			},
			beforeload:function(node){//2.单击节点前加载该节点的子节点
				var level=GridTree.list.instance.parentPanel.cmbLevel.getValue();
				if(node.id!=0){
					this.loader.dataUrl = '../gridauth/nodeItem.form?area_id='+node.id;
				}else{
					this.loader.dataUrl ='../gridareas/listAllRegion.form?level='+level;
				}
			},
			render:function(tree){
				tree.expand();// 1.展开
			}
		}
	});
	this.saveButton = new Ext.Button({
		text:"确定",
		handler:function(){this.saveOrUpdateOrder();},
		scope:this
	});
	
	this.colseButton = new Ext.Button({
		text:"关闭",
		handler:function(){
			this.destroy();
		},
		scope:this
	});
	var bodyHeight = document.documentElement.clientHeight-30;
	GridTree.list.superclass.constructor.call(this,{
		title:'功能权限树',
		width:300,
		height:350,
		modal:true,
		autoScroll:true,
		closeAction:'hide',
		layout:'border',
		buttonAlign:'right',
		buttons:[this.saveButton,this.colseButton],
		items:[this.treepanel]
	});
}
Ext.extend(GridTree.list,Ext.Window,{
	init: function(ids) {
//		for(var i=0;i<ids.length;i++){//默认选中
//			
//		}
	},
	saveOrUpdateOrder:function(){
		var nodeArray = this.treepanel.getChecked();
		var nodeIds=[];
		var nodeTexts=[];
		for(var i =0;i<nodeArray.length;i++){
			nodeTexts.push(nodeArray[i].text);
			nodeIds.push(nodeArray[i].id);
		}
		this.parentPanel.setIds(nodeIds,nodeTexts);
		this.hide();                
	}
});
